package com._520it.crm.web.controller;

import com._520it.crm.domain.Client;
import com._520it.crm.domain.Employee;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.ClientQueryObject;
import com._520it.crm.service.IClientService;
import com._520it.crm.service.IEmployeeService;
import com._520it.crm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 呆 on 2017/10/3.
 */
@Controller
@RequestMapping("/hiddenClient")
public class HiddenClientController {
    @Autowired
    private IClientService clientService;
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("")
    public String index(){
        return "hiddenClient";
    }


    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(ClientQueryObject qo)
    {
         return clientService.employeeQuery(qo);
    }

    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult save(Client client)
    {
        try {
            clientService.insert(client);
            return new AjaxResult(true,"保存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败!");
        }

    }

    @RequestMapping("/update")
    @ResponseBody
    public AjaxResult update(Client client)
    {
        try {
            clientService.updateByPrimaryClient(client);
            return new AjaxResult(true,"更新成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("更新失败!");
        }

    }

    /**
     * 潜在客户的转正
     * */
    @RequestMapping("/changeformal")
    @ResponseBody
    public AjaxResult changeformal(Long id)
    {
        try {
            clientService.changeFormalClientById(id);
            Client client = clientService.selectByPrimaryKey(id);
            Employee employee = employeeService.selectByPrimaryKey(client.getPrinciUser());
            return new AjaxResult(true,"转正成功!恭喜"+employee.getName()+",完成了对"+client.getRealName()+"这位客户的业务,今晚记得给他加鸡腿");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("转正失败!");
        }

    }

    /**
     * 潜在客户的放入资源池
     * */
    @RequestMapping("/resource")
    @ResponseBody
    public AjaxResult resource(Long id)
    {
        try {
            clientService.changeSourceFormalClientById(id);
            Client client = clientService.selectByPrimaryKey(id);
            Employee employee = employeeService.selectByPrimaryKey(client.getPrinciUser());

            return new AjaxResult(true,"将客户"+client.getRealName()+"转入资源池成功!,该客户不再由"+employee.getName()+"负责,成为了无负责人状态,等待其他负责人吸纳");
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("转入资源池失败!");
        }
    }




}
